package com.school.filesearch110;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.school.file.file;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import me.zhanghai.android.materialprogressbar.MaterialProgressBar;

public class SearchActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        Toolbar toolbar=(Toolbar)findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        final MaterialProgressBar materialProgressBar=(MaterialProgressBar)findViewById(R.id.progress_bar);
        if(materialProgressBar.getVisibility()==View.GONE)
        {
            materialProgressBar.setVisibility(View.VISIBLE);
        }
        Intent intent=getIntent();
        String content=intent.getStringExtra("content");
        List<file> l=new ArrayList<>();
        final FileAdapter adapter=new FileAdapter(l);
        BmobQuery<BmobFile> query=new BmobQuery<BmobFile>();
        query.addWhereEqualTo("name",content);
        FindListener fl=new FindListener<file>() {
            @Override
            public void done(List<file> list, BmobException e) {
                if(e==null){
                    int position=0;
                    for(file f :list)
                    {
                        adapter.add(position,f);
                        position++;
                    }
                    materialProgressBar.setVisibility(View.GONE);
                    if(position!=0)
                        adapter.notifyDataSetChanged();
                    else
                    {
                        TextView textView=(TextView)findViewById(R.id.error);
                        textView.setText("未找到符合条件的文件");
                    }
                }else{
                    materialProgressBar.setVisibility(View.GONE);
                    TextView textView=(TextView)findViewById(R.id.error);
                    textView.setText("出现错误,"+e.getMessage()+",错误代码:"+e.getErrorCode());
                    Log.i("bmob","失败："+e.getMessage()+","+e.getErrorCode());
                }
            }
        };
        query.findObjects(fl);
        RecyclerView recyclerView=(RecyclerView)findViewById(R.id.recycler_view);
        GridLayoutManager layoutManager=new GridLayoutManager(this,2);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);
    }
    public static void actionStart(Context context, String data)
    {
        Intent intent=new Intent(context,SearchActivity.class);
        intent.putExtra("content",data);
        context.startActivity(intent);
    }
    public class FileAdapter extends RecyclerView.Adapter<FileAdapter.myViewHolder> {
        private Context mContext;
        private List<file> mFile;
        public FileAdapter(List<file> fileList)
        {
            mFile=fileList;
        }
        public void add(int position,file f)
        {
            mFile.add(position,f);
        }
        @Override
        public myViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
        {
            if(mContext==null)
            {
                mContext=parent.getContext();
            }
            View view= LayoutInflater.from(mContext).inflate(R.layout.file_item,parent,false);
            return new myViewHolder(view);
        }
        @Override
        public void onBindViewHolder(myViewHolder holder, int position) {
            file f=mFile.get(position);
            holder.fileName.setText(f.getName());
            String temp=f.getType();
            switch (temp) {
               case "ppt":
                   Glide.with(mContext).load(R.drawable.ic_ppt).into(holder.filePic);
                   break;
               case "doc":
                   Glide.with(mContext).load(R.drawable.ic_doc).into(holder.filePic);
                   break;
               case "excel":
                   Glide.with(mContext).load(R.drawable.ic_excel).into(holder.filePic);
                   break;
               default:
            }
        }
        @Override
        public int getItemCount()
        {
            return mFile.size();
        }
        class myViewHolder extends RecyclerView.ViewHolder{
            CardView cardView;
            TextView fileName;
            ImageView filePic;
            public myViewHolder(View view)
            {
                super(view);
                cardView=(CardView)view;
                fileName=(TextView)view.findViewById(R.id.file_name);
                filePic=(ImageView)view.findViewById(R.id.file_image);
            }
        }
    }

}
